package com.cne.gmail.Gmail.Excutor;



import com.cne.gmail.Gmail.Handle.GmailHandle;
import com.cne.gmail.Gmail.Handle.PartsHandle;
import com.cne.gmail.Gmail.Util.OSSUtils;
import com.cne.gmail.Gmail.model.AttachMentIdModel;
import com.cne.gmail.Gmail.model.AttchmentModel;
import com.cne.gmail.Gmail.model.MessageModel;
import com.cne.gmail.Gmail.model.UserModel;
import com.google.api.client.util.Base64;
import com.google.api.services.gmail.Gmail;
import com.google.api.services.gmail.model.Message;
import com.google.api.services.gmail.model.MessagePartBody;
import com.google.api.services.gmail.model.MessagePartHeader;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.Callable;

public class ThreadUpload implements Callable {
    private static Logger logger = LoggerFactory.getLogger("ThreadUpload");
    private MessageModel messageModel;
    private final String URL="http://download.files.cnexps.com/";
    private String lable;
    private String id;
    private UserModel user;
    public ThreadUpload(String lable, UserModel user,String id){
       this.lable=lable;
       this.user=user;
       this.id=id;
    }

    @Override
    public MessageModel call() throws GeneralSecurityException, IOException {
           PartsHandle partsHandle=new PartsHandle();
           Gmail service= GmailHandle.service(user);
           Message message= service.users().messages().get("me",id).execute();
           messageModel=new MessageModel();
           List list= Lists.newArrayList();
           list.add(message.getPayload());
           List<AttachMentIdModel> attachMentIdModels= partsHandle.recurrenceGetData(list);
           List<AttchmentModel> attchmentModels=Lists.newArrayList();
           for (AttachMentIdModel attachMentIdModel:attachMentIdModels){
               //正文部分内容，不上传，保存到本地数据库
               if (attachMentIdModel.getData()!=null){
                   if(attachMentIdModel.getMedieType().equals("text/plain")){
                       byte[] fileByteArray = Base64.decodeBase64(attachMentIdModel.getData());
                       String text=new String(fileByteArray,"UTF-8");
                       messageModel.setPlain(text);
                   }else if(attachMentIdModel.getMedieType().equals("text/html")){
                       byte[] fileByteArray = Base64.decodeBase64(attachMentIdModel.getData());
                       String text=new String(fileByteArray,"UTF-8");
                       messageModel.setHtml(text);
                   }
               }else {
                   AttchmentModel attchmentModel=new AttchmentModel();
                   MessagePartBody attachPart=  service.users().messages()
                           .attachments()
                           .get("me",message.getId(),attachMentIdModel.getAttchMentId())
                           .execute();
                   byte[] fileByteArray = Base64.decodeBase64(attachPart.getData());
                   String fileName=   UUID.randomUUID().toString()+"/"+attachMentIdModel.getFileName();
                   String path = OSSUtils.getDefaultOSS().upLoadFileToOss(fileByteArray, fileName);
                   attchmentModel.setFileName(attachMentIdModel.getFileName());
                   attchmentModel.setUrl(URL+path);
                   attchmentModels.add(attchmentModel);
               }
           }
           List<MessagePartHeader> headers=message.getPayload().getHeaders();
           for (MessagePartHeader header:headers){
               if (header.getName().equals("Cc")){
                   messageModel.setCc(header.getValue());
               }else if (header.getName().equals("To")){
                   messageModel.setTo(header.getValue());
               }else if (header.getName().equals("From")){
                   messageModel.setFrom(header.getValue());
               }else if (header.getName().equals("Subject")){
                   messageModel.setSubject(header.getValue());
               }
           }
           messageModel.setLabel(lable);
           messageModel.setMessageId(message.getId());
           messageModel.setAttachMentModels(attchmentModels);
           SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
           long lt = new Long(message.getInternalDate().toString());
           Date date = new Date(lt);
           messageModel.setIntnetdate(lt);
           messageModel.setDate(simpleDateFormat.format(date));
           messageModel.setStatus(1);
           messageModel.setUserName(user.getUserName());
           messageModel.setEmailAddress(user.getEmailAddress());
           return messageModel;
       }

}
